package inits

import (
	"2301house/config"
	"2301house/global"
	"2301house/internal/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

func MysqlInits() {
	var err error
	var Mysql = config.ConFig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Mysql.User, Mysql.Password, Mysql.Host, Mysql.Port, Mysql.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Println("数据库链接失败")
		panic(err)
	}
	log.Println("mysql init success")

	global.DB.AutoMigrate(&model.User{}, &model.House{}, &model.HouseReservation{}, &model.Order{}, &model.ChatHistory{})
}
